import ArrayUtil from '@/ArrayUtil';
import BuffUtil from '@/BuffUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import EffectCommon from 'code/Core/Common/EffectCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';

const efp = '2415eb4e42b35d842018626cf54b4f8c.mdx';

/**
 * 圣言术 - 愈
 **/
export default class MS_HolyHeal extends AbilityBase {
    static instance: MS_HolyHeal;
    buffType: AppBuffType = {
        id: 'buff_MS_HolyHeal',
        art: this.art,
        name: this.abilityName,
        ubertip: '当onTakeDamage时产生治疗',
        dur: 20,
        targetAttach: 'overhead',
        targetArt: 'Abilities\\Spells\\NightElf\\Tranquility\\TranquilityTarget.mdl',
    };
    constructor(id: string) {
        super(id);
        MS_HolyHeal.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeDamage(this.onDamage.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let lv = this.getAbilityLevel(source);
        let val = 32 * lv + UnitCommon.取法强(source) * (0.65 + 0.35 * lv);
        DamageSystemInstance.applyHeal(target, source, val, this.id);
        EffectCommon.delyRemove(AddSpecialEffectTarget(efp, target, 'origin'), 1);
        BuffCommon.addBuff({
            target,
            caster: source,
            buffTypeId: this.buffType.id,
            ablityId: this.id,
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    onDamage(d: DamageEventData) {
        let buff = BuffUtil.getUnitBuff(d.target, this.buffType.id);
        if (buff == null) return;
        let lv = this.getAbilityLevel(buff.getCaster());
        let val = 32 * lv + UnitCommon.取法强(buff.getCaster()) * (0.65 + 0.35 * lv);
        DamageSystemInstance.applyHeal(d.target, buff.getCaster(), val, this.id);
        EffectCommon.delyRemove(AddSpecialEffectTarget(efp, d.target, 'origin'), 1);
        let us = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(d.target, 450);
        if (us.length <= 0) return;
        let dur = buff.getRemainingTime();
        let tar = ArrayUtil.randomElement(us);
        MovementCommon.TargetMissile({
            missile: 'Abilities\\Weapons\\FaerieDragonMissile\\FaerieDragonMissile.mdl',
            caster: buff.getCaster(),
            curX: GetUnitX(d.target),
            curY: GetUnitY(d.target),
            speed: 400,
            radian: 0.35,
            target: tar,
            onReach: this.onMissileReach.bind(this),
            other: dur,
        });
        buff.destroy();
    }
    onMissileReach(p: TargetMissileConfig) {
        // let buff = BuffUtil.addBuff(p.target, this.buffType.id, p.caster);
        // buff.durMS = p.other;
        BuffCommon.addBuff({
            target: p.target,
            caster: p.caster,
            buffTypeId: this.buffType.id,
            ablityId: this.id,
        });
    }
}
